import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:huajianshe_app/base/AppNavigator.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/res.dart';
import 'package:huajianshe_app/pages/search/SearchPage.dart';
import 'package:huajianshe_app/widget/SmartButton.dart';

import '../RoundedCornerWidget.dart';

class ContactAppBar extends StatefulWidget implements PreferredSizeWidget{

  final void Function(int)? onSelected;
  final String? title;

  const ContactAppBar(this.onSelected, {
    Key? key,
    this.title = '人脉'
  }) : super(key: key);


  @override
  State<StatefulWidget> createState() {
    return _ContactAppBarState();
  }

  @override
  Size get preferredSize => Size.fromHeight(48);
}

class _ContactAppBarState extends BaseState<ContactAppBar> {


  @override
  void initState() {
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(value: SystemUiOverlayStyle.dark,
        child: Material(color: Colors.white,
            child: SafeArea(bottom: false,child: Container(height: 48,
                child: Row(mainAxisSize: MainAxisSize.max, children: [
                  buildBack(),
                  Column(mainAxisSize: MainAxisSize.min, children: [
                    Text(widget.title ?? '', style: TS(21, C.gray1A, fontWeight: FontWeight.bold)),
                    H(5),
                    RoundedCornerWidget(child: W(0), width: 20, height: 3, radius: 1.5, backgroundColor: C.primary, borderWidth: 0,)
                  ],),
                  Spacer(),

                  PopupMenuButton<int>(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(13.0))
                    ),
                    itemBuilder: (context) {
                      return [
                        PopupMenuItem(value: 0, child: Container(width: 120, child: Row(
                            children: [LoadImage('ic_contact_wechat'), W(8), Text('交换微信', style: TS(14, C.gray333))]))),
                        PopupMenuItem(value: 1, child: Container(width: 120, child: Row(
                            children: [LoadImage('ic_contact_mobile'), W(8), Text('交换手机号', style: TS(14, C.gray333))]))),
                        PopupMenuItem(value: 2, child: Container(width: 120, child: Row(
                            children: [LoadImage('ic_contact_ipa'), W(8), Text('导出通讯录', style: TS(14, C.gray333))]))),
                        PopupMenuItem(value: 3, child: Container(width: 120, child: Row(
                            children: [LoadImage('ic_contact_delete'), W(8), Text(widget.title == '人脉' ? '我要拉黑' : '我要删除', style: TS(14, C.gray333))])))
                      ];
                    },
                    child: LoadImage('ic_contact_more'),
                    onSelected: (index) {
                      if (widget.onSelected != null) widget.onSelected!(index);
                    },
                  ),
                  // GestureDetector(onTap: widget.moreAction, child: LoadImage('ic_contact_more')),
                  W(15)
                ])
            ))
        )
    );
  }

  Widget buildBack() {
    return widget.title == '人脉' ? W(16) : IconButton(
      onPressed: () async {
        FocusManager.instance.primaryFocus?.unfocus();
        final isBack = await Navigator.maybePop(context);
        if (!isBack) {
          await SystemNavigator.pop();
        }
      },
      tooltip: 'Back',
      padding: const EdgeInsets.all(12.0),
      icon: LoadImage(
        'ic_black_arrow',
        // color: backImgColor ?? ThemeUtils.getIconColor(context),
      ),
    );
  }
}